type("Hello, World")
str
type("Hello, World")
str
type(())
tuple
type([])
list
type(set())
set
type({})
dict
# 파이썬에서는 복소수 i를 j로 표현함
type(2+3j)
complex
="Hello, world!"
message=42
n=2.71
eprint(n)
print(message)
42
Hello, world!
=4.2
length=3.5
height=length*height
areaprint(area)
14.700000000000001
# 변수는 방문앞에 이름만 바꾼것... 새로운 변수에 기존 변수를 할당하고, 기존 변수를 변경하면 새로운 변수도 영향받음
=[1,2,3]
a=a
b0]=10
a[print(b)
[10, 2, 3]
=1.0
x=1.0
X=1.0
X1=1.0
x1=1.0
dell# not allowed
# x:, 1X, X1-1, for
=1,3.14,'a'
x,y,zprint(x,y,z)
1 3.14 a
=1
x=1.0
y=float(1)
z=1.234e-5
xxprint(x,y,z,xx)
print(type(z),type(float(True)))
1 1.0 1.0 1.234e-05
<class 'float'> <class 'float'>
# float 타입은 근사치를 사용하기때문에 사용에 유의
1-0.9==0.1
False
float('inf')
inf
=1j
x=2+3j
y=complex(1)
zprint(x,y,z,y.real,y.imag)
print(type(y.imag))
1j (2+3j) (1+0j) 2.0 3.0
<class 'float'>
# Integer는 float와 달리 정확하다
=2**127+2**65
x x
170141183460469231768580791863303208960
print(bool(1),bool(1.2),bool(-1),bool(0.1))
True True True True
print(bool(0),bool(0.0),bool(0.0000),bool(None),bool(""),bool(()))
False False False False False False
3>4
False
=10
a=20 b
# 나누기 연산은 항상 float로 반환됨을 유의
print(a+b,a-b,a*b,b/a)
30 -10 200 2.0
# 나머지 연산자
%a b
0
# 몫 연산자
//a b
2
# 거듭제곱 연산자, 우선순위가 가장 높음
**b a
100000000000000000000
print(a==b,a!=b,a>b,a<b,a>=b,a<=b)
False True False True False True
=0
t+=1
t t
1
/=2
t t
0.5
*=2
t t
1.0
print(True and False, True or False, not True)
False True False
# 논리연산자는 container가 bool이 아닌 경우 용법이 다름
# or은 가장 먼저나오는 True값을 반환, and는 가장 먼저나오는 false값을 반환
print('' or 'abc',0 or 1, 'abc' or '','abc' or 'ab')
print(1 and 0, 'abc' and 0, None and '' and 0)
abc 1 abc abc
0 0 None
# 거듭제곱 > 덧샘뺄샘 > 나눗셈, 몫, 나머지 > 부등호 > 논리
1+3/2
2.5
2400//500*500+2000%500
2000
2400//500)*500+(2000%500) (
2000
# built-in
print(abs(-1),round(1.111,1))
1 1.1
# functional in math module
import math
# factorial, floor, isfinite, isinf, isnan, trunc
# exp, log, log10, sqrt, cos, sin, tan, degrees, radians, pi, e
print(
1.11),
math.ceil(10),
math.log(10,10),
math.log(1),
math.exp(
math.e,
math.pi,
math.trunc(math.pi),1),
math.degrees(
math.cos(math.pi) )
2 2.302585092994046 1.0 2.718281828459045 2.718281828459045 3.141592653589793 3 57.29577951308232 -1.0
# print(object1, object2, ... , sep=' ', end='\n',...)
print(1,2,3,4)
print(1,2,3,4,sep='*')
print(1,2,3,4,end='END')
print(1,2,3,4,sep='')
1 2 3 4
1*2*3*4
1 2 3 4END1234
# formatting
=5; y=10
xprint('The value of x is {} and y is {}'.format(x,y))
print('I love {2} and {1}, but hate {0}'.format('cucumber','butter','bread'))
The value of x is 5 and y is 10
I love bread and butter, but hate cucumber
# Input value is a string
=input('Enter a number:')
numprint(num)
print(int(num))
10
10